package com.zz.chenko.common.utils;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.zz.chenko.sys.dao.UserDao;
import com.zz.chenko.sys.entity.User;
import com.zz.chenko.sys.service.UserService;

/**
 * 用户工具类
 *
 * @author Zhong
 * @since 2017年9月13日
 */
public class UserUtil {

    private static Logger logger = LoggerFactory.getLogger(UserUtil.class);

    static UserDao userDao = SpringContextHolder.getBean(UserDao.class);
    static UserService userService = SpringContextHolder.getBean(UserService.class);

    public static User getUser() {
        User user = new User();
        Object o = SecurityUtils.getSubject().getPrincipal();
        if (o != null) {
            logger.info("loginName: {}", o.toString());
            user = getUserByLoginName(o.toString());
            logger.info("user: {}", JSON.toJSONString(user));
        }
        return user;
    }

    public static Long getUserId() {
        return getUser().getId();
    }

    public static User getUserByLoginName(String loginName) {
        User user = new User();
        user.setLoginName(loginName);
        return userDao.selectOne(user);
    }
}
